package com.Guo.system.controller;


import com.Guo.handler.BusinessException;
import com.Guo.response.Result;
import com.Guo.response.ResultCode;
import com.Guo.system.entity.Department;
import com.Guo.system.service.DepartmentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Guo
 * @since 2020-12-03
 */

@Api(value = "部门管理")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

//     @GetMapping("/findDeptAndCount")
//     @ApiOperation(value = "查询部门及人数",notes = "从部门表中分组查询用户人数")
//     public Result findDeptAndCount(){
//         List<Department> all = departmentService.findAll();
//         if (all==null){
//             throw new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
//         }
//         return Result.ok().data("departments",all);
//     }

    @GetMapping("/PageDeptAndCount")
    @ApiOperation(value = "分页查询部门及人数", notes = "从部门表中分页查询用户人数")
    public Result PageDeptAndCount(@RequestParam(required = false,defaultValue = "1") Integer current,
                                   @RequestParam(required = false) Integer size,
                                   @RequestParam(required = false) String name) {
        if (size==null){
            int count = departmentService.count();
            size=count;
        }
            Page<Department> page = new Page<>(current, size);
            Page<Department> deptAll = departmentService.findDeptAll(page, name);
            return Result.ok().data("DeptPage", deptAll);
    }
}

